package org.kca.yarmour.data;

import java.util.ArrayList;
import java.util.List;

import org.kca.yarmour.YAException;
import org.kca.yarmour.data.GlobalData.QuestType;
import org.kca.yarmour.yadata.ItemType;
import org.kca.yarmour.yaquest.QuestInfoType;

public class QuestDetails
{
    private String id, name;
    private QuestType type;
    private String description, longDescription, spawnLocation;
    private int frequency;
    private List<Info> passTasks, failTasks, acceptRewards, passRewards, failRewards, prereqs;
    public boolean hidden;
    
    public QuestDetails(String id, String name)
    {
        this.id = id;
        this.name = name;
        passTasks = new ArrayList<Info>();
        failTasks = new ArrayList<Info>();
        acceptRewards = new ArrayList<Info>();
        passRewards = new ArrayList<Info>();
        failRewards = new ArrayList<Info>();
        prereqs = new ArrayList<Info>();
    }
    
    /**
     * Gets the id
     * @return the id
     */
    public String getId()
    {
        return id;
    }

    /**
     * Gets the name
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Gets the type
     * @return the type
     */
    public QuestType getType()
    {
        return type;
    }

    /**
     * Gets the description
     * @return the description
     */
    public String getDescription()
    {
        return description;
    }

    /**
     * Gets the longDescription
     * @return the longDescription
     */
    public String getLongDescription()
    {
        return longDescription;
    }

    /**
     * Gets the frequency
     * @return the frequency
     */
    public int getFrequency()
    {
        return frequency;
    }
    
    /**
     * Checks if this quest is hidden. A hidden quest will not appear on the quest list
     * and will be auto-completed or failed.
     * @return true if this quest is hidden
     */
    public boolean isHidden()
    {
        return hidden;
    }

    /**
     * Gets the spawnLocation
     * @return the spawnLocation
     */
    public String getSpawnLocation()
    {
        return spawnLocation;
    }

    /**
     * Gets the passTasks
     * @return the passTasks
     */
    public List<Info> getPassTasks()
    {
        return passTasks;
    }

    /**
     * Gets the failTasks
     * @return the failTasks
     */
    public List<Info> getFailTasks()
    {
        return failTasks;
    }

    /**
     * Gets the passRewards
     * @return the passRewards
     */
    public List<Info> getPassRewards()
    {
        return passRewards;
    }

    /**
     * Gets the acceptRewards
     * @return the acceptRewards
     */
    public List<Info> getAcceptRewards()
    {
        return acceptRewards;
    }

    /**
     * Gets the failRewards
     * @return the failRewards
     */
    public List<Info> getFailRewards()
    {
        return failRewards;
    }

    /**
     * Gets the prerequisites
     * @return the prerequisites
     */
    public List<Info> getPrereqs()
    {
        return prereqs;
    }
    
    /**
     * Checks if this quest is failable. A failable quest can be failed if the fail condition
     * is met before the pass condition
     * @return true if the quest is failable
     */
    public boolean isFailable()
    {
        return !failTasks.isEmpty();
    }

    /**
     * Creates a item based on details specified in XML
     * @param qtype the XML quest type
     * @return a quest object
     */
    public static QuestDetails createFromXmlType(org.kca.yarmour.yaquest.QuestType qtype)
    {
        QuestDetails q = new QuestDetails(qtype.getId(), qtype.getName());
        q.description = qtype.getDesc();
        q.longDescription = qtype.getLongDesc();
        q.type = QuestType.getQuestType(qtype.getType());
        if (q.type == null)
        {
            throw new YAException("Illegal quest type: " + qtype.getType());
        }
        if (qtype.getFrequency() != null)
        {
            q.frequency = qtype.getFrequency();
        }
        if (qtype.isHidden() != null)
        {
            q.hidden = qtype.isHidden();
        }
        else
        {
            q.hidden = false;
        }
        q.spawnLocation = qtype.getSpawnLocation();
        for (QuestInfoType qinfo : qtype.getPrereq())
        {
            q.getPrereqs().add(Info.createFromXmlType(qinfo));
        }
        for (QuestInfoType qinfo : qtype.getPasstask())
        {
            q.getPassTasks().add(Info.createFromXmlType(qinfo));
        }
        for (QuestInfoType qinfo : qtype.getFailtask())
        {
            q.getFailTasks().add(Info.createFromXmlType(qinfo));
        }
        for (QuestInfoType qinfo : qtype.getAcceptreward())
        {
            q.getAcceptRewards().add(Info.createFromXmlType(qinfo));
        }
        for (QuestInfoType qinfo : qtype.getPassreward())
        {
            q.getPassRewards().add(Info.createFromXmlType(qinfo));
        }
        for (QuestInfoType qinfo : qtype.getFailreward())
        {
            q.getFailRewards().add(Info.createFromXmlType(qinfo));
        }
        
        return q;
    }
}
